//
//  GetConnection.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/9.
//  Copyright (c) 2015年 L-G. All rights reserved.
//

#import "GetConnection.h"

@implementation GetConnection

+ (void)startGETConnection:(NSString *)url parmaters:(NSDictionary *)parmater connectionBlock:(connectionBlock)block
{
    NSURL *strUrl = [NSURL URLWithString:url];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setTimeoutInterval:kTimeOutInterval];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    [self startMonitoringNetworkStatus];
    manager.securityPolicy.allowInvalidCertificates = YES;
    [manager GET:strUrl.absoluteString parameters:parmater success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        block(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self badNetWork];
        NSLog(@"网络错误%@",error);
    }];
}
+ (void)startMonitoringNetworkStatus
{
    AFNetworkReachabilityManager *reachabilityManager = [AFNetworkReachabilityManager sharedManager];
    [reachabilityManager startMonitoring];
    [reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch ((NSInteger)status) {
            case AFNetworkReachabilityStatusNotReachable:{
                NSLog(@"网络连接中断");
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                NSLog(@"当前WiFi网络");
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                NSLog(@"当前3G网络");
                break;
            }
        }
    }];
}
//返回网络状态
+ (AFNetworkReachabilityStatus)networkReachabilityStatus{
    return [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
}
+ (void)badNetWork{
    if ([[self class] networkReachabilityStatus] != -1) {
//        [MBProgressHUD showErrorWithStatus:@"网络异常"];
    }
}
@end
